package no.zsoft.faktura.admin.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import no.zsoft.faktura.admin.domain.Authority;
import no.zsoft.faktura.admin.domain.User;
import no.zsoft.faktura.admin.service.UserServices;

import org.primefaces.event.FlowEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

@Controller
@Scope("session")
public class TenantWizard implements Serializable {

	@Autowired
	private UserServices userServices;

	private String tenant;
	private boolean valid;

	private boolean created;
	private User user;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init(){
		valid=false;
		created=false;
		tenant=null;
		user=new User();
	}

	public void validate() {
		System.err.println("validating");
		
		if (!StringUtils.hasLength(tenant) || tenant.length() < 5) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Tenant name Should be atleast 5 charachter!", ""));
			valid = false;
		}else if(org.apache.commons.lang.StringUtils.isNumeric(tenant)){
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Tenant name Should contain atleast 1 alphabetic!", ""));
			valid = false;

		}
		else if (!org.apache.commons.lang.StringUtils.isAlphanumeric(tenant)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Tenant name contains illegal charachter!", ""));
			valid = false;
		}

		else if (!userServices.isTenantNameAvailable(tenant)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Tenant name not available!", ""));
			valid = false;
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							"Tenant name is valid!", ""));
			valid = true;
		}

	}

	

	public void createTenant() {
		System.err.println("Creating " + tenant);
		try{
			userServices.createTenant(tenant);
			setCreated(true);
			setValid(false);
		}catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"An error accured during creating DataBase", ""));
			setCreated(false);
			e.printStackTrace();
			
		}
		
	}
	public void saveUser(){
		List<String> rols=new ArrayList<String>();
		rols.add(Authority.ROLE_ADMIN.name());
		rols.add(Authority.ROLE_IRAN.name());
		rols.add(Authority.ROLE_USER.name());
		user.setTenant(tenant);
		userServices.creatNewUser(user, rols);
	}

	/**
	 * @return the tenant
	 */
	public String getTenant() {
		return tenant;
	}

	/**
	 * @param tenant
	 *            the tenant to set
	 */
	public void setTenant(String tenant) {
		this.tenant = tenant;
	}

	/**
	 * @return the valid
	 */
	public boolean isValid() {
		return valid;
	}

	/**
	 * @param valid
	 *            the valid to set
	 */
	public void setValid(boolean valid) {
		this.valid = valid;
	}

	/**
	 * @return the created
	 */
	public boolean isCreated() {
		return created;
	}

	/**
	 * @param created the created to set
	 */
	public void setCreated(boolean created) {
		this.created = created;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

}
